home *** CD-ROM | disk | FTP | other *** search
/ NeXT Education Software Sampler 1992 Fall / NeXT Education Software Sampler 1992 Fall.iso / Programming / Source / winterp-1.13 / src-server / xlisp / xlpp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-04  |  4.0 KB  |  153 lines

  1. /* -*-C-*-
  2. ********************************************************************************
  3. *
  4. * File:         xlpp.c
  5. * RCS:          $Header: xlpp.c,v 1.4 91/03/24 22:25:17 mayer Exp $
  6. * Description:  xlisp pretty printer
  7. * Author:       David Michael Betz
  8. * Created:      
  9. * Modified:     Fri Oct  4 04:08:00 1991 (Niels Mayer) mayer@hplnpm
  10. * Language:     C
  11. * Package:      N/A
  12. * Status:       X11r5 contrib tape release
  13. *
  14. * WINTERP Copyright 1989, 1990, 1991 Hewlett-Packard Company (by Niels Mayer).
  15. * XLISP version 2.1, Copyright (c) 1989, by David Betz.
  16. *
  17. * Permission to use, copy, modify, distribute, and sell this software and its
  18. * documentation for any purpose is hereby granted without fee, provided that
  19. * the above copyright notice appear in all copies and that both that
  20. * copyright notice and this permission notice appear in supporting
  21. * documentation, and that the name of Hewlett-Packard and David Betz not be
  22. * used in advertising or publicity pertaining to distribution of the software
  23. * without specific, written prior permission.  Hewlett-Packard and David Betz
  24. * make no representations about the suitability of this software for any
  25. * purpose. It is provided "as is" without express or implied warranty.
  26. *
  27. * HEWLETT-PACKARD AND DAVID BETZ DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
  28. * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
  29. * IN NO EVENT SHALL HEWLETT-PACKARD NOR DAVID BETZ BE LIABLE FOR ANY SPECIAL,
  30. * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  31. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  32. * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  33. * PERFORMANCE OF THIS SOFTWARE.
  34. *
  35. * See ./winterp/COPYRIGHT for information on contacting the authors.
  36. * Please send modifications, improvements and bugfixes to mayer@hplabs.hp.com
  37. * Post XLISP-specific questions/information to the newsgroup comp.lang.lisp.x
  38. *
  39. ********************************************************************************
  40. */
  41. static char rcs_identity[] = "@(#)$Header: xlpp.c,v 1.4 91/03/24 22:25:17 mayer Exp $";
  42.  
  43.  
  44. #include "xlisp.h"
  45.  
  46. /* external variables */
  47. extern LVAL s_stdout;
  48. extern int xlfsize;
  49.  
  50. /* local variables */
  51. static int pplevel,ppmargin,ppmaxlen;
  52. static LVAL ppfile;
  53.  
  54. /* xpp - pretty-print an expression */
  55. LVAL xpp()
  56. {
  57.     LVAL expr;
  58.  
  59.     /* get expression to print and file pointer */
  60.     expr = xlgetarg();
  61.     ppfile = (moreargs() ? xlgetfile() : getvalue(s_stdout));
  62.     xllastarg();
  63.  
  64.     /* pretty print the expression */
  65.     pplevel = ppmargin = 0; ppmaxlen = 40;
  66.     pp(expr); ppterpri(ppfile);
  67.  
  68.     /* return nil */
  69.     return (NIL);
  70. }
  71.  
  72. /* pp - pretty print an expression */
  73. LOCAL pp(expr)
  74.   LVAL expr;
  75. {
  76.     if (consp(expr))
  77.     pplist(expr);
  78.     else
  79.     ppexpr(expr);
  80. }
  81.  
  82. /* pplist - pretty print a list */
  83. LOCAL pplist(expr)
  84.   LVAL expr;
  85. {
  86.     int n;
  87.  
  88.     /* if the expression will fit on one line, print it on one */
  89.     if ((n = flatsize(expr)) < ppmaxlen) {
  90.     xlprint(ppfile,expr,TRUE);
  91.     pplevel += n;
  92.     }
  93.  
  94.     /* otherwise print it on several lines */
  95.     else {
  96.     n = ppmargin;
  97.     ppputc('(');
  98.     if (atom(car(expr))) {
  99.         ppexpr(car(expr));
  100.         ppputc(' ');
  101.         ppmargin = pplevel;
  102.         expr = cdr(expr);
  103.     }
  104.     else
  105.         ppmargin = pplevel;
  106.     for (; consp(expr); expr = cdr(expr)) {
  107.         pp(car(expr));
  108.         if (consp(cdr(expr)))
  109.         ppterpri();
  110.     }
  111.     if (expr != NIL) {
  112.         ppputc(' '); ppputc('.'); ppputc(' ');
  113.         ppexpr(expr);
  114.     }
  115.     ppputc(')');
  116.     ppmargin = n;
  117.     }
  118. }
  119.  
  120. /* ppexpr - print an expression and update the indent level */
  121. LOCAL ppexpr(expr)
  122.   LVAL expr;
  123. {
  124.     xlprint(ppfile,expr,TRUE);
  125.     pplevel += flatsize(expr);
  126. }
  127.  
  128. /* ppputc - output a character and update the indent level */
  129. LOCAL ppputc(ch)
  130.   int ch;
  131. {
  132.     xlputc(ppfile,ch);
  133.     pplevel++;
  134. }
  135.  
  136. /* ppterpri - terminate the print line and indent */
  137. LOCAL ppterpri()
  138. {
  139.     xlterpri(ppfile);
  140.     for (pplevel = 0; pplevel < ppmargin; pplevel++)
  141.     xlputc(ppfile,' ');
  142. }
  143.  
  144. /* flatsize - compute the flat size of an expression */
  145. LOCAL int flatsize(expr)
  146.   LVAL expr;
  147. {
  148.     xlfsize = 0;
  149.     xlprint(NIL,expr,TRUE);
  150.     return (xlfsize);
  151. }
  152.